﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using NCC_SOP.app_codes;
using System.Data.SqlClient;
using System.Data;

namespace NCC_SOP.Admin
{
    public partial class fastFact_Add : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Session["currUploadedFileURL"] = "";
            }
        }

        protected void uploadFile_Click(Object sender, EventArgs e)
        {
            webUtilities util = new webUtilities();
            string targetFolder = Server.MapPath("../staticReports/");
            try
            {
                Session["currUploadedFileURL"] = util.BindValidResults(this.rptLinks, targetFolder, this.repeaterValidResults, this.uploadError);
            }
            catch (Exception e2)
            {
                this.uploadError.Text = "File Size Too Big! Pls reduce file size to <2MB";
                this.uploadError.Visible = true;
            }
        }

        protected void insertRecord(Object sender, EventArgs e)
        {
            bool checkpoint1 = false;
            bool checkpoint2 = false;
            bool checkpoint3 = false;

            string test = (string)Session["currUploadedFileURL"];
            //insert the 
            string connectionString = "server=ncdrovm;database=ncc-sop;uid=sa;password=lifebook;";

            int tabelID = 0;
            using (SqlConnection SqlConnection = new SqlConnection(connectionString))
            {
                string selectSQL = "INSERT INTO [ncc-sop].[dbo].[MetadataTables]([TableName],[TableDescription],[TableLabel],[createdBy],[createdOn],[status],[fileName],[isRpt],[categories]) VALUES(@tableName,@TableDescription,@TableLabel,@createdBy,@createdOn,@status,@fileName,@isRpt,@categories);SELECT @@Identity";

                SqlCommand cmd = new SqlCommand(selectSQL, SqlConnection);

                cmd.Parameters.Add("@tableName", SqlDbType.VarChar);
                cmd.Parameters.Add("@TableDescription", SqlDbType.VarChar);
                cmd.Parameters.Add("@TableLabel", SqlDbType.VarChar);
                cmd.Parameters.Add("@createdBy", SqlDbType.VarChar);
                cmd.Parameters.Add("@createdOn", SqlDbType.DateTime);
                cmd.Parameters.Add("@status", SqlDbType.VarChar);
                cmd.Parameters.Add("@fileName", SqlDbType.VarChar);
                cmd.Parameters.Add("@categories", SqlDbType.VarChar);
                cmd.Parameters.Add("@isRpt", SqlDbType.SmallInt);

                cmd.Parameters["@tableName"].Value = this.rptName.Text;
                cmd.Parameters["@TableDescription"].Value = this.rptDescription.Text;
                cmd.Parameters["@TableLabel"].Value = this.rptName.Text;
                cmd.Parameters["@createdBy"].Value = "admin";
                cmd.Parameters["@isRpt"].Value = this.isRpt.SelectedValue;
                cmd.Parameters["@categories"].Value = "fact";

                DateTime currDate = DateTime.Now;

                cmd.Parameters["@createdOn"].Value = currDate.Date.ToString("yyyy-MM-dd HH:mm:ss");
                cmd.Parameters["@status"].Value = this.rpt_status.SelectedValue;

                if (Convert.ToInt32(this.isRpt.SelectedValue) == 0)
                {
                    cmd.Parameters["@fileName"].Value = test;
                }
                else
                {
                    cmd.Parameters["@fileName"].Value = this.rptURL.Text;
                }

                SqlConnection.Open();
                tabelID = Convert.ToInt32(cmd.ExecuteScalar());
                checkpoint1 = true;
            }


            //INSERT THE DATA defintion information: dataSource
            //dataSource attributeID is 1
            webUtilities util = new webUtilities();
            checkpoint2 = util.insertTableAttributeValues(tabelID, 4, this.DS.SelectedValue);
            checkpoint3 = util.insertTableAttributeValues(tabelID, 5, this.rptType.SelectedValue);


            if (checkpoint1 && checkpoint2 && checkpoint3)
            {
                Response.Redirect("fastFact_admin.aspx");
            }
            else
            {
                this.errorMsg.Visible = true;
                if (!checkpoint1)
                {
                    this.errorMsg.Text = "checkpoint1 ERROR.";
                }
                else if (!checkpoint2)
                {
                    this.errorMsg.Text = "checkpoint2 ERROR.";
                }
                else if (!checkpoint3)
                {
                    this.errorMsg.Text = "checkpoint3 ERROR.";
                }
            }
        }
    }
}